package com.learn.book;

import java.util.concurrent.Semaphore;

public class CustomCheckWindow {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(5);

        for (int i = 1; i <=5;i++) {
            new SecurityCheckThread(i,semaphore).start();
        }
    }
    private static class SecurityCheckThread extends Thread {
        private int seq;

        private Semaphore semaphore;

        public SecurityCheckThread(int seq, Semaphore semaphore) {
            this.seq = seq;
            this.semaphore = semaphore;
        }

        @Override
        public void run() {
            try {
                semaphore.acquire();
                System.out.println("No." + seq +"乘客，正在安检中...");
                if (seq % 2 == 0) {
                    Thread.sleep(1000);
                    System.out.println("No." +seq + "乘客，身份可疑，不能出国");
                }
            } catch(Exception e) {
                e.printStackTrace();
            } finally {
                semaphore.release();
                System.out.println("No." + seq + "乘客已完成服务");
            }
        }
    }


}

